/**  

* @ClassName: GaoDeUtil  

* @Description: TODO(这里用一句话描述这个类的作用)  

* @author windows  

* @date 2021年7月20日  

*/  
package com.xhkj.util;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhkj.model.GaodeLocation;
import com.xhkj.model.Geocodes;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author windows
 *
 */
public class GaoDeUtil {
	/**
     * 输入地址返回识别后的信息
     *
     * @param address
     * @return 返回的类gaodelocation，详见类
     */
    public static GaodeLocation getLocatoin(String address) {
        GaodeLocation location = null;
        if (address != null) {
            try {
                String result = "";
                location = new GaodeLocation();
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(address).build();
                try {
                    Response response = client.newCall(request).execute();
                    result = response.body().string();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(result);
 
                JSONObject jsonObject = JSONObject.parseObject(result);
 
                // 解析json
                location.setStatus(jsonObject.getString("status"));
                location.setInfo(jsonObject.getString("info"));
                location.setCount(jsonObject.getString("count"));
                List<Geocodes> geocodes = new ArrayList<>();
                JSONArray jsonArray = jsonObject.getJSONArray("pois");
                // 遍历解析出来的结果
                if ((jsonArray != null) && (jsonArray.size() > 0)) {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jo = (JSONObject) jsonArray.get(i);
                        Geocodes go = new Geocodes();
                        go.setFormatted_address(jo.getString("address"));
                        go.setProvince(jo.getString("pname"));
                        go.setCitycode(jo.getString("citycode"));
                        go.setCity(jo.getString("cityname"));
                        go.setDistrict(jo.getString("adname"));
                        // 地址所在的乡镇
                        JSONArray ts = jo.getJSONArray("township");
                        if (ts != null && ts.size() > 0) {
                            go.setTownship(ts.getString(0));
                        }
                        // 地址编号
                        go.setAdcode(jo.getString("adcode"));
                        // 街道
                        JSONArray jd = jo.getJSONArray("street");
                        if (jd != null && jd.size() > 0) {
                            go.setStreet(jd.getString(0));
                        }
                        // 号码
                        JSONArray hm = jo.getJSONArray("number");
                        if (hm != null && hm.size() > 0) {
                            go.setStreet(hm.getString(0));
                        }
                        go.setLocation(jo.getString("location"));
                        go.setLevel(jo.getString("level"));
                        go.setName(jo.getString("name"));
                        go.setTel(jo.getString("tel").replace("[]", ""));
                        geocodes.add(go);
                    }
                }
                location.setGeocodes(geocodes);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return location;
    }
}
